(function () {
    'use strict';

    /**
     * @ngdoc function
     * @name webApp.controller:WxappCtrl
     * @description
     * # WxappCtrl
     * Controller of the webApp
     */
    angular.module('webApp')
        .controller('WxappCtrl', WxappCtrl);

    WxappCtrl.$inject = ['$scope', 'dataService', 'helper', '$location', '$timeout', '$rootScope', 'ERP_CONFIG'];

    function WxappCtrl($scope, dataService, helper, $location, $timeout, $rootScope, ERP_CONFIG) {

        $scope.listAction = function () {
            $scope.xtotal = 0;
            $scope.page_Size = 10;
            $scope.getList();
        }
        $scope.fabulistAction = function () {
            $scope.template_id = $location.search().template_id;
            $scope.page_Size = 10;
            $scope.getfabulist(1);
        }

        //获取列表
        $scope.getfabulist = function (pageNo) {
            var query = {
                content: {
                    page_no: pageNo,
                    page_size: $scope.page_Size ? $scope.page_Size : 10,
                    template_id: $scope.template_id,
                }
            };

            dataService.batchGet(query).then(function (list) {
                if (list && list.batchlog_list) {
                    $scope.wxapps = list.batchlog_list;
                    $scope.xtotal = list.total_count;
                }
            });
        }

        //批量上传代码
        $scope.batchup = function () {

            dataService.batchUpload({ content: { template_id: $scope.template_id } }).then(function (re) {
                if (re.Msg) {
                    helper.message("错误：" + re.Msg,"error");
                } else {

                    helper.message("批量操作中！");
                    $scope.getfabulist(1);

                }

                $timeout(function () {
                    $scope.getfabulist();
                }, 3000);
            });

        }

        //获取仓库列表
        $scope.getList = function (pageNo) {
            $scope.pageNo = pageNo || 1;
            var query = {
                page_no: $scope.pageNo,
                page_size: $scope.pageSize
            };

            if ($scope.appid) {
                query.query = { "appid": $scope.appid };
            }
            dataService.isvWxappGet(query).then(function (list) {
                if (list && list.wxapps) {
                    $scope.wxapps = list.wxapps;
                    $scope.xtotal = list.total_count;
                }
            });
        }

        $scope.getwxapplist = function () {
            dataService.WxGettemplatedraftlist(ERP_CONFIG.BackAppId).then(function (result) {
                console.error("result", result);
            });
        }


        $scope.addAction = function () {
            $scope.templetype = 1;
            $scope.gettempleList();
            var appid = $location.search()["appid"];
            if (appid) {
                dataService.isvWxappOne(appid).then(function (result) {
                    if (result.appid) {
                        $scope.wxapp = result;
                    } else {
                        helper.message("错误：" + result.Msg,"error");
                    }
                })
            } else {
                $scope.wxapp = {
                    app_type: 1,
                    template_id: 1,
                    statu: -1,
                    version: "v0.0.1",
                    description: "描述",
                }
                // $scope.getwxapplist();
            }
        }

        $scope.save = function () {
            var method = $scope.wxapp.appid ? "isvWxappUpdate" : "isvWxappAdd";
            dataService[method]($scope.wxapp).then(function (result) {
                if (result.Msg) {
                    helper.message("错误：" + result.Msg,"error");
                } else {
                    gourl("#/wxapp/list");
                }
            });
        }

        //获取草稿列表
        $scope.gettempleList = function () {
            if ($scope.templetype) {
                dataService.wxapptempList($scope.templetype).then(function (result) {
                    if (result.Msg) {
                        helper.message("错误：" + result.Msg,"error");
                    } else {
                        $scope.wxapps = result.draft_list ? result.draft_list : result.template_list;
                        $scope.wxapps = _.sortBy($scope.wxapps, 'create_time').reverse();
                    }
                });
            }
        }

        $scope.del = function (id) {
            dataService.wxappdeltemplated(id).then(function (result) {
                if (result.Msg) {
                    helper.message("错误：" + result.Msg,"error");
                } else {
                    helper.message("成功");
                    $scope.gettempleList();
                }
            });
        }
        $scope.copy = function (id,statu) {
            _.map($scope.wxapps, function (da, key) {
                if (da.template_id == id) {
                    $scope.wxapp.template_id = id;
                    $scope.wxapp.version = da.user_version;
                    $scope.wxapp.description = da.user_desc;
                    $scope.wxapp.statu=statu==2?1:-1;
                    var msg=statu==2?"正式发布？":"发布为草稿？";
                    helper.myConfirm(msg,function(){
                        $scope.save();
                    });
                }
            });
        }
        $scope.addtotemplate = function (draft_id) {
            if (draft_id) {
                dataService.addtotemplate(draft_id).then(function (result) {
                    if (result.Msg) {
                        helper.message("错误：" + result.Msg,"error");
                    } else {
                        helper.message("成功");
                        $scope.gettempleList();
                    }
                });
            }
        }
        $scope.delete = function (appid) {
            dataService.isvWxappDelete(appid).then(function (result) {
                if (result.Msg) {
                    helper.message("错误：" + result.Msg,"error");
                } else {
                    $scope.getList();
                }
            });
        }
    }
})();